package com.fs.excel;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.poi.ss.usermodel.Row;
import org.hellojavaer.poi.excel.utils.ExcelProcessController;
import org.hellojavaer.poi.excel.utils.ExcelUtils;
import org.hellojavaer.poi.excel.utils.read.ExcelReadContext;
import org.hellojavaer.poi.excel.utils.read.ExcelReadFieldMapping;
import org.hellojavaer.poi.excel.utils.read.ExcelReadRowProcessor;
import org.hellojavaer.poi.excel.utils.read.ExcelReadSheetProcessor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author shuai.fang
 * @date：2016-11-29
 * @description：
 */
public class ExcelTest {

    public static void main(String[] args) throws FileNotFoundException {
        File f=new File("/Users/fangshuai/Downloads/a.xlsx");
        InputStream inputStream= new FileInputStream(f);

        ExcelReadSheetProcessor<SysUser> sheetProcessor = new ExcelReadSheetProcessor<SysUser>() {

            @Override
            public void beforeProcess(ExcelReadContext<SysUser> context) {

            }

            @Override
            public void process(ExcelReadContext<SysUser> context, List<SysUser> list) {
                System.out.println(JSONObject.toJSONString(list, SerializerFeature.WriteDateUseDateFormat));
            }

            @Override
            public void onException(ExcelReadContext<SysUser> context, RuntimeException e) {

            }

            @Override
            public void afterProcess(ExcelReadContext<SysUser> context) {

            }
        };
        ExcelReadFieldMapping fieldMapping = new ExcelReadFieldMapping();
        fieldMapping.put("area", "area");
        fieldMapping.put("city", "city");
        fieldMapping.put("realName", "realName");
        fieldMapping.put("mobile", "mobile");
        fieldMapping.put("email", "email");
        fieldMapping.put("idno", "idno");
        fieldMapping.put("userName", "userName");
        fieldMapping.put("status", "status");

        sheetProcessor.setSheetIndex(0);// required.it can be replaced with 'setSheetName(sheetName)';
        sheetProcessor.setStartRowIndex(1);//
        // sheetProcessor.setRowEndIndex(3);//
        sheetProcessor.setTargetClass(SysUser.class);// required
        sheetProcessor.setFieldMapping(fieldMapping);// required
        sheetProcessor.setPageSize(2);//
        sheetProcessor.setTrimSpace(true);
        sheetProcessor.setHeadRowIndex(0);
        final List<SysUser> list = new ArrayList<SysUser>();
        sheetProcessor.setRowProcessor(new ExcelReadRowProcessor<SysUser>() {

            public SysUser process(ExcelProcessController controller, ExcelReadContext<SysUser> context, Row row,
                                   SysUser t) {
                System.out.println(t);
                list.add(t);
                return t;
            }
        });

        ExcelUtils.read(inputStream, sheetProcessor);
        System.out.println(list.size());
    }

}
